import express, { Request, Response, NextFunction } from "express"
import { checkError } from "../../utils/token"
import { CommondityManageCtx } from "../../context/CommondityManageCtx"
import { AddCommdity, AddTeaItemParam, EditCommdity, EnableCommdityParams, GetTeaItemQuery, GetTeaListParam, TeaItemListType } from "@Tea/model"
import { catchError, useCheckAndResponse } from "../../utils/common"
import { LoggedInRequest } from "../../types"

const commodityManage = express.Router()

commodityManage.post('/pageList', async (req: Request, res: Response) => { 
  try {
    const params = req.body as GetTeaListParam
    const data = await CommondityManageCtx.getInstance().getPageList(params)
  
    setTimeout(() => {
      res.send({
        code: 1,
        msg: '查询成功',
        ...data
      })
    }, Math.random() * 1000)
  } catch (ex) {
    res.status(500).send({
      code: 0,
      msg: checkError(ex)
    })
  }
})

commodityManage.post('/addCommdity', async (req: LoggedInRequest, res: Response) => { 
  try {
    const params = req.body as AddCommdity
    const result = await CommondityManageCtx.getInstance().addCommdity(params, req.userInfo.userId)
    
    useCheckAndResponse(res, result[0], '新增成功')
  } catch (ex) {
    res.status(500).send({
      code: 0,
      msg: checkError(ex)
    })
  }
})

commodityManage.post('/editCommdity', async (req: LoggedInRequest, res: Response) => { 
  try {
    const params = req.body as EditCommdity
    const result = await CommondityManageCtx.getInstance().editCommdity(params, req.userInfo.userId)
    
    useCheckAndResponse(res, result[0], '编辑成功')
  } catch (ex) {
    res.status(500).send({
      code: 0,
      msg: checkError(ex)
    })
  }
})

commodityManage.put("/enableCommdity", async (req: LoggedInRequest, res: Response) => {
  try {
    const params = req.body as EnableCommdityParams
    const result = await CommondityManageCtx.getInstance().enableCommdity(params, req.userInfo.userId)
    useCheckAndResponse(res, result[0], params.enable === 1 ? "启用成功" : "停用成功")

  } catch (ex) {
    res.status(500).send({
      code: 0,
      msg: checkError(ex)
    })
  }
})

commodityManage.get('/getTeaItems', async (req: LoggedInRequest, res: Response) => { 
  try {
    const params = req.query as any as GetTeaItemQuery
    const data = await CommondityManageCtx.getInstance().getTeaItems(params.teaId)
  
    setTimeout(() => {
      res.send({
        code: 1,
        msg: '查询成功',
        data
      })
    }, Math.random() * 1000)
  } catch (ex) {
    res.status(500).send({
      code: 0,
      msg: checkError(ex)
    })
  }
})

commodityManage.post('/addTeaItem', async (req: LoggedInRequest, res: Response) => { 
  try {
    const params = req.body as AddTeaItemParam
    const result = await CommondityManageCtx.getInstance().addTeaItem(params)
    
    useCheckAndResponse(res, result[0], '新增详情成功', true)
  } catch (ex) {
    res.status(500).send({
      code: 0,
      msg: checkError(ex)
    })
  }
})

commodityManage.delete('/delTeaItem', async (req: LoggedInRequest, res: Response) => { 
  try {
    const itemId = req.query.itemId as any as TeaItemListType['itemId']
    const result = await CommondityManageCtx.getInstance().delTeaItem(itemId)
    
    useCheckAndResponse(res, result[0], '删除成功', true)
  } catch (ex) {
    res.status(500).send({
      code: 0,
      msg: checkError(ex)
    })
  }
})

export default commodityManage